﻿using System;
using System.Windows.Forms;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Configuration;

namespace PDFProtect
{
    class Permission
    {
        /*
         Initialize checked items from "App.config".
         Below is comparing String[] and String[]. Then, get the items we need to check.
        */
        public static void initializeCheckedItems(CheckedListBox listBox)
        {
            
            Int32 countPermissionSetting = 0;
            String[] settings = { "ALLOW_ASSEMBLY", "ALLOW_COPY", "ALLOW_DEGRADED_PRINTING", "ALLOW_FILL_IN", "ALLOW_MODIFY_ANNOTATIONS", "ALLOW_MODIFY_CONTENTS", "ALLOW_PRINTING", "ALLOW_SCREENREADERS" };
            foreach (String setting in settings)
            {
                if (Convert.ToBoolean(ConfigurationManager.AppSettings[setting]))
                {
                    listBox.SetItemChecked(countPermissionSetting, true);
                }
                countPermissionSetting++;
            }
        }
        /*
         Get permission from "listbox".
         Below is comparing String[] and String. Then, can know permissions we need to process.
        */
        public static Int32 getPermission(CheckedListBox listBox)
        {
            Int32 countPermissionSetting = 0;
            Int32 exportPermissions = new Int32();
            Int32[] Permissions = { PdfWriter.ALLOW_ASSEMBLY, PdfWriter.ALLOW_COPY, PdfWriter.ALLOW_DEGRADED_PRINTING, PdfWriter.ALLOW_FILL_IN, PdfWriter.ALLOW_MODIFY_ANNOTATIONS, PdfWriter.ALLOW_MODIFY_CONTENTS, PdfWriter.ALLOW_PRINTING, PdfWriter.ALLOW_SCREENREADERS };
            foreach (Int32 permision in Permissions)
            {
                if (listBox.GetItemChecked(countPermissionSetting))
                {
                    exportPermissions = exportPermissions | permision;
                }
                countPermissionSetting++;
            }
            return exportPermissions;
        }

        public static bool isPasswordProtected(string pdfFullname)
        {
            try
            {
                PdfReader pdfReader = new PdfReader(pdfFullname);
                return false;
            }
            catch (BadPasswordException)
            {
                return true;
            }
        }
    }
}
